﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Media;
using Fireball.Extensions;

namespace Fireball.OpenXML.Word
{
    /// <summary>
    /// Store the properties for a run Style
    /// </summary>
    public class StyleRunProperties : OpenXMLEntity
    {
        // <a:b a:val="on"/>
        public bool Bold
        {
            get;
            set;
        }

        public bool Italic
        {
            get;
            set;
        }

        public bool Underline
        {
            get;
            set;
        }

        // <a:bCs a:val=""/>
        public bool BoldComplexScript
        {
            get;
            set;
        }


        // <a:bdr
        public TextBorder Border
        {
            get;
            set;
        }

        // <a:cs a:val =""/>
        public bool UseComplexScript
        {
            get;
            set;
        }

        // TODO :EastAsiaLayout
        /*
          public ? EastAsiaLayout
         * */

        // <a:oMath a:val=""/>
        /// <summary>
        /// Office OpenXML Math
        /// </summary>
        public bool Math
        {
            get;
            set;
        }


        public int? FontSize
        {
            get;
            set;
        }

        public int? ComplexFontSize
        {
            get;
            set;
        }

        //Can't be nested
        public RunRevisionInformation Revision
        {
            get;
            set;
        }

        public Color? Foreground
        {
            get;
            set;
        }


        public override System.Xml.Linq.XContainer ToXml()
        {
            XElement xProperties = new XElement(NS.WM + "rPr");

            if (this.Bold)
                xProperties.Add(new XElement(NS.WM + "b",
                    new XAttribute(NS.WM + "val", "1")));

            if (this.Italic)
                xProperties.Add(new XElement(NS.WM + "i",
                    new XAttribute(NS.WM + "val", "1")));

            if (this.Underline)
                xProperties.Add(new XElement(NS.WM + "u",
                    new XAttribute(NS.WM + "val", "1")));

            xProperties.Add(new XElement(NS.WM + "bCs",
                new XAttribute(NS.WM + "val", this.BoldComplexScript.ToOnOff())));

            if (this.FontSize != null)
            {
                xProperties.Add(new XElement(NS.WM + "sz",
                    new XAttribute(NS.WM + "val", this.FontSize.Value)));
            }

            if (this.Border != null)
                xProperties.Add(this.Border.ToXml());

            xProperties.Add(new XElement(NS.WM + "cs",
                new XAttribute(NS.WM + "val", this.UseComplexScript.ToOnOff())));
            //TODO: <a:eastAsianLayout/>

            xProperties.Add(new XElement(NS.WM + "oMath",
                new XAttribute(NS.WM + "val", this.Math.ToOnOff())));


            if (this.ComplexFontSize != null)
            {
                xProperties.Add(new XElement(NS.WM + "szCs",
                    new XAttribute(NS.WM + "val", this.ComplexFontSize.Value)));
            }
            if (this.Foreground != null)
            {
                xProperties.Add(new XElement(NS.WM + "color",
                    new XAttribute(NS.WM + "val", this.Foreground.Value.ToHexFormat())));
            }
            if (this.Revision != null)
                xProperties.Add(this.Revision.ToXml());

            return xProperties;
        }

        internal void ParseInternal(XElement rPr)
        {
            XName check = NS.WM + "rPr";
            if (rPr.Name != check)
                throw new Exception("Invalid XElement");

            var bold = rPr.Element(NS.WM + "b");
            if (bold != null)
            {
                this.Bold = true;
            }

            var italic = rPr.Element(NS.WM + "i");
            if (italic != null)
            {
                this.Italic = true;
            }

            var underline = rPr.Element(NS.WM + "u");
            if (underline != null)
            {
                this.Underline = true;
            }

            var boldCs = rPr.Element(NS.WM + "bCs");
            if (boldCs != null)
            {
                this.BoldComplexScript = true;
            }

            var cs = rPr.Element(NS.WM + "cs");
            if (cs != null)
            {
                this.UseComplexScript = true;
            }

            var oMath = rPr.Element(NS.WM + "oMath");
            if (oMath != null)
            {
                this.Math = true;
            }

            var border = rPr.Element(NS.WM + "bdr");

            if (border != null)
            {
                this.Border = TextBorder.Parse(border);
            }

            var size = rPr.Element(NS.WM + "sz");

            if (size != null)
            {
                this.FontSize = Convert.ToInt32(size.GetOfficeValAttribute());
            }

            var sizeCs = rPr.Element(NS.WM + "szCs");

            if (sizeCs != null)
            {
                this.ComplexFontSize = Convert.ToInt32(sizeCs.GetOfficeValAttribute());
            }

            var color = rPr.Element(NS.WM + "color");

            if (color != null)
            {
                var c = color.GetOfficeValAttribute();

                this.Foreground = (Color)ColorConverter.ConvertFromString("#" +c);
            }

            var rPrChange = rPr.Element(NS.WM + "rPrChange");

            if (rPrChange != null)
            {
                this.Revision = RunRevisionInformation.Parse(rPrChange);
            }
        }

        public static StyleRunProperties Parse(XElement rPr)
        {
            var st = new StyleRunProperties();
            st.ParseInternal(rPr);
            return st;
        }
    }
}
